package us.quizgame;

import java.awt.Color;
import java.awt.EventQueue;

import javax.sound.sampled.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.ImageIcon;

import us.quizgame.quiz.NormalQuizGame;
import us.quizgame.quiz.SurvivalQuizGame;


public class Menu extends JFrame {

	private String gameTitle = "The XTR3M3 Quiz Game";
	private String backgroundImage;
	private int score;
	public static boolean alreadyPlayed = false;
	
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					Menu frame = new Menu();
					frame.setVisible(true);
					frame.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Menu() {
		setTitle("The XTR3M3 Quiz Game");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.setBackground(Color.black); 
		
		JButton btnStartGame = new JButton("Start Game");
		btnStartGame.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				takeQuizRegular();
			}
		});
		btnStartGame.setBounds(150, 117, 250, 50);
		contentPane.add(btnStartGame);
		
		JButton btnSurvivalMode = new JButton("Survival Mode");
		btnSurvivalMode.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				takeQuizSurvival();
			}
		});
		btnSurvivalMode.setBounds(150, 178, 250, 50);
		contentPane.add(btnSurvivalMode);
		
		JButton btnViewScores = new JButton("View Scores");
		btnViewScores.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				viewScores();
			}
		});
		btnViewScores.setBounds(150, 239, 250, 50);
		contentPane.add(btnViewScores);
		
		JButton btnHelp = new JButton("Help");
		btnHelp.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				getHelp();
			}
		});
		btnHelp.setBounds(150, 361, 250, 50);
		contentPane.add(btnHelp);
		
		JButton btnSubmitQuestion = new JButton("Submit Question");
		btnSubmitQuestion.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnSubmitQuestion.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				submitQuestion();
			}
		});
		btnSubmitQuestion.setBounds(150, 300, 250, 50);
		contentPane.add(btnSubmitQuestion);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(Menu.class.getResource("/us/quizgame/images/av-20034.png")));
		lblNewLabel.setBounds(194, 6, 150, 99);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setIcon(new ImageIcon(Menu.class.getResource("/us/quizgame/images/blood-drip.gif")));
		lblNewLabel_1.setBounds(0, 0, 193, 289);
		contentPane.add(lblNewLabel_1);
		
		JLabel label = new JLabel("New label");
		label.setIcon(new ImageIcon(Menu.class.getResource("/us/quizgame/images/blood-drip.gif")));
		label.setBounds(345, -3, 205, 292);
		contentPane.add(label);
		
			
			//play main theme      
        	if(alreadyPlayed == false)
        	{
        			try{
        			AudioInputStream stream = AudioSystem.getAudioInputStream(Menu.class.getResource("/us/quizgame/media/RainingBlood.wav"));
        			AudioFormat format = stream.getFormat();
        			DataLine.Info info = new DataLine.Info(Clip.class, format);
        			Clip clip = (Clip) AudioSystem.getLine(info);
        			clip.open(stream);
        			clip.loop(Clip.LOOP_CONTINUOUSLY);
        			clip.start();
        			}
        
        			catch(Exception e)
        			{
        			}
        	}
        	
        	alreadyPlayed = true;
        }
	
	
	//Methods to launch menu options
	public void takeQuizRegular() {
		JOptionPane.showMessageDialog(null, "You have 5 seconds to answer each question for a total of 10 questions!");
		NormalQuizGame.main(null);
		dispose();	
	}
	
	public void takeQuizSurvival() {
		JOptionPane.showMessageDialog(null, "You have 5 seconds to answer each question miss a question and the game is over!");
		SurvivalQuizGame.main(null);
		dispose();	
	}
	
	public void getHelp() {
		Help.main(null);
		dispose();
	}
	
	public void submitQuestion()
	{
		SubmitQuestions.main(null);
		dispose();
	}
	
	public void viewScores()
	{
		ScoreCard.main(null);
		dispose();
	}
}
